# @Author: Eric Ito
# @Date: 1/29/2009
# @Name: Project Euler Problem 26

"""
find value of d for 1/d that has largest recurring decimal
"""
import decimal as dc

def isRecurring(val):
    i = 8 # set this value to make sure recurring numbers that
          # start with 001xxxx are not mistakenly reported as 0
    rec = 0
    longest = 0

    while 1:
        if val[:i/2] == val[i/2:i]:
            if len(val[:i/2]) > longest:
                longest = len(val[:i/2])
                rec = val[:i/2]
                return str(rec)#,longest
        i += 2
        if i >= len(val):
            return str(rec)


def main():
    dc.getcontext().prec = 2001
    i = 983
    num,cycle = 0,""
    while i <= 1000:
        val = dc.Decimal(1)/dc.Decimal(str(i))
        rec = isRecurring(str(val)[2:])
        if len(rec) > len(cycle):
            cycle = rec
            #num = d
            print "d =",i,"recurring num =",cycle,"length =",len(cycle)
        i += 1

if __name__ == "__main__":
    main()